package com.guet.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guet.common.utils.R;
import com.guet.main.entity.Room;
import com.guet.main.entity.RoomReservation;
import com.guet.main.entity.RoomTimeSlice;
import com.guet.main.entity.User;
import com.guet.main.entity.vo.ReservationVo;
import com.guet.main.entity.vo.RoomReservationVo;
import com.guet.main.service.RoomReservationService;
import com.guet.main.service.RoomService;
import com.guet.main.service.RoomTimeSliceService;
import com.guet.main.service.UserService;
import com.guet.main.utils.JWTUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * room_reservation 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-07-29
 */
@RestController
@RequestMapping("/main/room-reservation")
@CrossOrigin
public class RoomReservationController {
    @Autowired
    private RoomReservationService roomReservationService;
    @Autowired
    private RoomTimeSliceService roomTimeSliceService;
    @Autowired
    private UserService userService;


    @PostMapping("/reserve")
    @ApiOperation("预约")
    public R reserve(@RequestBody ReservationVo reservationVo,HttpServletRequest request){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        reservationVo.setUserId(userId);
        boolean reserve = roomReservationService.reserve(reservationVo);
        return reserve?R.ok():R.error();
    }

    @GetMapping("/cancel/{id}")
    @ApiOperation("取消预约")
    public R cancel(@PathVariable String id){
        Boolean res = roomReservationService.cancel(id);
        return res?R.ok():R.error();
    }

    @GetMapping("/cancelBySliceId/{sliceId}")
    @ApiOperation("通过时间片ID来取消预约")
    public R cancelBySliceId(@PathVariable String sliceId){
        QueryWrapper<RoomReservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("slice_id",sliceId);
        List<RoomReservation> list = roomReservationService.list(queryWrapper);
        if(list!=null){
            Boolean res = roomReservationService.cancel(list.get(0).getId());
            return res?R.ok():R.error();
        }else {
            return R.error();
        }
    }

    @GetMapping("/list")
    @ApiOperation("获取所有的预约")
    public R list(){
        QueryWrapper<RoomReservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("gmtCreate");
        List<RoomReservation> list = roomReservationService.list(null);
        List<RoomReservationVo> resList = new ArrayList<>();
        for (RoomReservation roomReservation : list) {
            RoomReservationVo roomReservationVo = new RoomReservationVo();
            BeanUtils.copyProperties(roomReservation, roomReservationVo);
            RoomTimeSlice roomTimeSlice = roomTimeSliceService.getById(roomReservationVo.getSliceId());
            roomReservationVo.setRoomId(roomTimeSlice.getRoomId());
            roomReservationVo.setRoomName(roomTimeSlice.getRoomName());
            User user = userService.getById(roomReservationVo.getUserId());
            roomReservationVo.setUserName(user.getName());
            roomReservationVo.setSliceDate(roomTimeSlice.getSliceDate());
            roomReservationVo.setBeginTime(roomTimeSlice.getBeginTime());
            roomReservationVo.setEndTime(roomTimeSlice.getEndTime());
            resList.add(roomReservationVo);
        }
        return R.ok().data("list",resList);
    }

    @GetMapping("/getByUserId")
    @ApiOperation("通过用户ID获取")
    public R getByUserId(HttpServletRequest request){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        QueryWrapper<RoomReservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByAsc("gmt_create");
        List<RoomReservation> list = roomReservationService.list(queryWrapper);
        List<RoomReservationVo> resList = new ArrayList<>();
        for (RoomReservation roomReservation : list) {
            RoomReservationVo roomReservationVo = new RoomReservationVo();
            BeanUtils.copyProperties(roomReservation, roomReservationVo);
            RoomTimeSlice roomTimeSlice = roomTimeSliceService.getById(roomReservationVo.getSliceId());
            roomReservationVo.setRoomId(roomTimeSlice.getRoomId());
            roomReservationVo.setRoomName(roomTimeSlice.getRoomName());
            User user = userService.getById(roomReservationVo.getUserId());
            roomReservationVo.setUserName(user.getName());
            roomReservationVo.setSliceDate(roomTimeSlice.getSliceDate());
            roomReservationVo.setBeginTime(roomTimeSlice.getBeginTime());
            roomReservationVo.setEndTime(roomTimeSlice.getEndTime());
            resList.add(roomReservationVo);
        }
        return R.ok().data("list",resList);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("通过ID获取")
    public R getById(@PathVariable String id){
        RoomReservation byId = roomReservationService.getById(id);
        return R.ok().data("reservation",byId);
    }
}

